
from selenium import webdriver
from selenium.webdriver.edge.service import Service
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from damai.utils.driver import *


class Base:

    def __init__(self,name='edge'):
        """
        :param path: 换成自己的路径
        """
        self.driver = get_driver(name)

    def open_url(self,url):
        """
        打开网站并窗口最大化
        :param url: 打开的网址
        """
        self.driver.get(url)
        self.driver.maximize_window()

    def find_ele(self,loc):
        """
        查找并返回单个元素
        :param loc: 元素定位方法loc=('id','chat-textarea')
        :return: 找到的单个元素
        """
        element = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable(loc),
            "指定元素没有定位到"
        )
        return element


    def click_ele(self,loc):
        """
        对元素进行点击操作
        :param loc: 元素定位方法
        """
        ele = self.find_ele(loc)
        ele.click()

    def input_value(self,loc,value):
        """
        输入操作
        :param loc: 元素定位方法
        :param value: 要输入的值
        """
        ele = self.find_ele(loc)
        ele.send_keys(value)

    def switch_to_iframe(self,loc):
        """
        切换到内置的网页
        :param loc:
        """
        ele = self.find_ele(loc)
        self.driver.switch_to.frame(ele)

    def quit_all(self):
        self.driver.quit()

if __name__ == '__main__':
    v5 = Base()
    v5.open_url('https://v5.crmeb.net/admin/login?redirect=%2Fadmin%2Findex/')
    v5.click_ele((By.TAG_NAME,'button'))
    v5.click_ele((By.XPATH,'//div[@class="open-image"]'))
    v5.click_ele((By.XPATH,'//li[@title="商品"]'))
    v5.click_ele((By.XPATH,'//li[@class="el-menu-item"]/span[text()="商品分类"]'))
    v5.click_ele((By.XPATH,'//div[@class="el-card__body"]/button'))
    v5.click_ele((By.XPATH,'//div[@class="fc-upload-btn"]'))
    v5.switch_to_iframe((By.TAG_NAME,"iframe"))
    v5.click_ele((By.XPATH,'//*[text()="上传图片"]'))

    v5.input_value((By.XPATH,'//div/div/input[@class="el-upload__input"]'),
                   r'D:\developer\projectStudy\6d3648420f85cfec84816ac9a2e710b8.jpg'
    )
    sleep(3)
